package ru.eas.graph;

import ru.eas.structure.Heap;

import java.util.*;

public class DijkstraDistance {

    public static void main(String[] args) {
        String testString = "4 4\n" +
                "1 2 1\n" +
                "4 1 2\n" +
                "2 3 2\n" +
                "1 3 5\n" +
                "1 3";
        Scanner scanner = new Scanner(testString);

//        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        DirectedGraph graph = new DirectedGraph(n);
        for (int i = 0; i < m; i++) {
            graph.addEdge(scanner.nextInt(), scanner.nextInt(), scanner.nextInt());
        }
        int vertex1 = scanner.nextInt();
        int vertex2 = scanner.nextInt();
        System.out.println(graph.dijkstraDistance(vertex1, vertex2));
    }


    static class DirectedGraph {

        class Edge {
            int vertex;
            int weight;

            Edge(int vertex, int weight) {
                this.vertex = vertex;
                this.weight = weight;
            }
        }

        public static final int INFINITY_DISTANCE = Integer.MAX_VALUE - 10000;

        private List<List<Edge>> vertices;
        private int[] distances;
        private int edgeCount;

        public DirectedGraph(int vertexCount) {
            vertexCount++;
            vertices = new ArrayList<List<Edge>>(vertexCount);
            for (int i = 0; i < vertexCount; i++) {
                vertices.add(new ArrayList<Edge>());
            }
            distances = new int[vertexCount];
        }

        public void addEdge(int vertex1, int vertex2) {
            this.addEdge(vertex1, vertex2, 1);
        }

        public void addEdge(int vertex1, int vertex2, int weight) {
            Edge edge = new Edge(vertex2, weight);
            vertices.get(vertex1).add(edge);
            edgeCount++;
        }

        public int dijkstraDistance(int vertex1, int vertex2) {
            for (int i = 1; i < vertices.size(); i++) {
                distances[i] = INFINITY_DISTANCE;
            }
            distances[vertex1] = 0;
            Heap priorityQueue = new Heap(edgeCount / vertices.size() + 1, new DistanceComparator());
            for (int i = 1; i < vertices.size(); i++) {
                  priorityQueue.insert(i);
            }
            while(!priorityQueue.isEmpty()) {
                int v1 = priorityQueue.extract();
                for (Edge edge : vertices.get(v1)) {
                    int v2 = edge.vertex;
                    relax(v1, v2, edge.weight);
                    priorityQueue.changePriority(v2);
                }
            }
            return distances[vertex2] != INFINITY_DISTANCE ? distances[vertex2] : -1;
        }

        private void relax(int v1, int v2, int weight) {
            int distance = distances[v1] + weight;
            if (distances[v2] > distance) {
                distances[v2] = distance;
            }
        }

        class DistanceComparator implements Comparator<Integer> {
            @Override
            public int compare(Integer i1, Integer i2) {
                int x = Integer.MAX_VALUE;
                int y = Integer.MAX_VALUE;
                if (i1 < distances.length) {
                    x = distances[i1];
                }
                if (i2 < distances.length) {
                    y = distances[i2];
                }
                return (x < y) ? -1 : ((x == y) ? 0 : 1);
            }
        }
    }
}
